diff --git a/library/media_sessions.php b/library/media_sessions.php index 682e8b0..06bfdeb 100644 --- a/library/media_sessions.php +++ b/library/media_sessions.php @@ -1,708 +1,710 @@ class MediaSessions { var $dispatcher_port = 25061; var $sessions = array(); var $summary = array(); var $domain_statistics = array(); var $timeout = 3; function MediaSessions ($dispatcher='',$allowedDomains=array(),$filters=array()) { $this->dispatcher = $dispatcher; $this->filters = $filters; $this->allowedDomains = $allowedDomains; $this->getUserAgentPictures(); } function getUserAgentPictures (){ global $userAgentImages; global $userAgentImagesFile; if (!isset($userAgentImagesFile)) { $userAgentImagesFile="phone_images.php"; } require_once($userAgentImagesFile); $this->userAgentImages = $userAgentImages; } function connectSocket() { if (!strlen($this->dispatcher)) return false; if (preg_match("/^(tls|tcp):(.*):(.*)$/",$this->dispatcher,$m)) { $hostname = $m[1].'://'.$m[2]; $port = $m[3]; $target= 'tcp://'.$m[2].':'.$m[3]; $transport= $m[1]; $this->mp_tls_cert_file = '/etc/cdrtool/mediaproxy.'.$m[2].'.pem'; if ($m[1] == 'tls') { if (!file_exists($this->mp_tls_cert_file)) { printf ("
Error: mediaproxy certificate file %s does not exist. \n",$this->mp_tls_cert_file); return false; } $tls_options=array('ssl' => array('local_cert' => $this->mp_tls_cert_file)); $context=stream_context_create($tls_options); } else { $context=stream_context_create(array()); } } else { printf ("
Error: MediaProxy dispatcher '%s' must be in the form: tls:hostname:port or tcp:hostname:port",$this->dispatcher); return false; } if ($fp = stream_socket_client ($target, $errno, $errstr,$this->timeout,STREAM_CLIENT_CONNECT,$context)) { if ( $transport == "tls") { if ( !stream_socket_enable_crypto($fp, true, STREAM_CRYPTO_METHOD_SSLv3_CLIENT)){ printf ("
Error connecting to %s: (Could not enable crypto) \n", $target); return false; } } return $fp; } else { printf ("
Error connecting to %s: %s (%s) \n",$target,$errstr,$errno); return false; } } function fetchSessionFromNetwork() { // get sessions from MediaProxy2 dispatcher if (!$fp = $this->connectSocket()) return array(); fputs($fp, "sessions\r\n"); $line = fgets($fp); return json_decode($line); } function fetchSummaryFromNetwork() { // get summary from MediaProxy2 dispatcher if (count($this->allowedDomains)) return array(); if (!$fp = $this->connectSocket()) return array(); fwrite($fp, "summary\r\n"); $line = fgets($fp); fclose($fp); return json_decode($line,true); } function getSessions () { $_sessions=$this->fetchSessionFromNetwork(); if (count($this->allowedDomains)) { foreach ($_sessions as $_session) { list($user1,$domain1)=explode("@",$_session->from_uri); list($user2,$domain2)=explode("@",$_session->to_uri); if (preg_match("/^(.*):/",$domain1,$m)) $domain1=$m[1]; if (!in_array($domain1,$this->allowedDomains) && !in_array($domain2,$this->allowedDomains)) { continue; } $this->domain_statistics[$domain1]['sessions']++; $this->domain_statistics['total']['sessions']++; foreach ($_session->streams as $streamInfo) { list($relay_ip,$relay_port)=explode(":",$streamInfo->caller_local); $_relay_statistics[$relay_ip]['stream_count'][$streamInfo->media_type]++;; if ($_session->duration) { $session_bps=($streamInfo->caller_bytes+$streamInfo->callee_bytes)/$_session->duration*8; $_relay_statistics[$relay_ip]['bps_relayed'] = $_relay_statistics[$relay_ip]['bps_relayed'] + $session_bps; } $this->domain_statistics[$domain1]['caller'] = $this->domain_statistics[$domain1]['caller'] + intval($streamInfo->caller_bytes/$_session->duration*2); $this->domain_statistics['total']['caller']=$this->domain_statistics['total']['caller']+intval($streamInfo->caller_bytes/$_session->duration*2); $this->domain_statistics[$domain1]['callee']=$this->domain_statistics[$domain1]['callee']+intval($streamInfo->callee_bytes/$_session->duration*2); $this->domain_statistics['total']['callee']=$this->domain_statistics['total']['callee']+intval($streamInfo->callee_bytes/$_session->duration*2); } $_relay_statistics[$relay_ip]['session_count']++; $_sessions2[] = $_session; } } else { foreach ($_sessions as $_session) { list($user1,$domain1)=explode("@",$_session->from_uri); list($user2,$domain2)=explode("@",$_session->to_uri); if (preg_match("/^(.*):/",$domain1,$m)) $domain1=$m[1]; $this->domain_statistics[$domain1]['sessions']++; $this->domain_statistics['total']['sessions']++; foreach ($_session->streams as $streamInfo) { if ($_session->duration) { $this->domain_statistics[$domain1]['caller']=$this->domain_statistics[$domain1]['caller']+intval($streamInfo->caller_bytes/$_session->duration*2); $this->domain_statistics['total']['caller']=$this->domain_statistics['total']['caller']+intval($streamInfo->caller_bytes/$_session->duration*2); $this->domain_statistics[$domain1]['callee']=$this->domain_statistics[$domain1]['callee']+intval($streamInfo->callee_bytes/$_session->duration*2); $this->domain_statistics['total']['callee']=$this->domain_statistics['total']['callee']+intval($streamInfo->callee_bytes/$_session->duration*2); } } } $_sessions2 = $_sessions; } if (count($this->allowedDomains)) { foreach (array_keys($_relay_statistics) as $_ip) { $this->relay_statistics[]=array('ip' => $_ip, 'bps_relayed' => $_relay_statistics[$_ip]['bps_relayed'], 'session_count' => $_relay_statistics[$_ip]['session_count'], 'stream_count' => $_relay_statistics[$_ip]['stream_count'], 'status' => 'ok', 'uptime' => 'unknown' ); } } if (strlen($this->filters['user'])) { foreach ($_sessions2 as $_session) { $user=$this->filters['user']; if (preg_match("/$user/",$_session->from_uri) || preg_match("/$user/",$_session->to_uri)) { $this->sessions[] = $_session; } } } else { $this->sessions = $_sessions2; } } function getSummary () { if (count($this->allowedDomains)){ if (is_array($this->relay_statistics)) { $this->summary = $this->relay_statistics; } } else { $this->summary = $this->fetchSummaryFromNetwork(); } } function showSearch() { printf ("
", $_SERVER['PHP_SELF'], $_REQUEST['user'] ); print ""; } function showHeader() { print "Address | Relayed traffic | Sessions | Streams | Status |
---|
Address | Version | Uptime | Relayed traffic | Sessions | Streams | Status |
---|
%d | %s | %s | %d | %s | %s | ||
%d | %s | %s | %s | %s | %d | %s | %s |
Callers (".count($this->sessions).") | Phones | Media Streams | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Relay caller | Relay callee | Status | Type/Codec | Duration | Bytes Caller |
Bytes Called |
||||||
|
"; if ($fromImage == 'unknown.png') { print ""; } else if ($fromImage == 'asterisk.png') { print ""; } else { print " "; } print " | "; if ($toImage == 'unknown.png') { print ""; } else if ($toImage == 'asterisk.png') { print ""; } else { print " "; } print " | "; $duration = $this->normalizeTime($session->duration); if (count($session->streams) > 0) { foreach ($session->streams as $streamInfo) { $status = $streamInfo->status; $statusClass = ""; if ($status=="idle" || $status=='hold') { $idletime = $this->normalizeTime($streamInfo->timeout_wait); $status = sprintf("%s %s", $status, $idletime); } else if ($status == "closed" ) { $statusClass = "muted"; } $caller = $streamInfo->caller_remote; $callee = $streamInfo->callee_remote; $relay_caller = $streamInfo->caller_local; $relay_callee = $streamInfo->callee_local; if (substr_count($relay_caller,":") == 1) { // Probaly ipv4 $relay_caller_data = explode(":",$relay_caller); $relay_caller = $this->ip2host($relay_caller_data[0]).":".$relay_caller_data[1]; } if (substr_count($relay_callee,":") == 1) { // Probaly ipv4 $relay_callee_data = explode(":",$relay_callee); $relay_callee = $this->ip2host($relay_callee_data[0]).":".$relay_callee_data[1]; } $codec = $streamInfo->caller_codec; $type = $streamInfo->media_type; if ($caller == '?.?.?.?:?') { $caller = ''; // a dash $align1 = 'center'; } else { $align1 = 'left'; } if ($callee == '?.?.?.?:?') { $callee = ''; // a dash $align2 = 'center'; } else { $align2 = 'left'; } if ($codec == 'Unknown') { $codec = ''; // a dash } else { $codec = "$codec"; } if ($type == 'Unknown') { $type = ''; // a dash } else if ($type == 'video') { $type = "$type"; } else if ($type == 'audio') { $type = "$type"; } else { $type = "$type"; } $bytes_in1 = $this->normalizeBytes($streamInfo->caller_bytes); $bytes_in2 = $this->normalizeBytes($streamInfo->callee_bytes); print "$caller | $relay_caller | $relay_callee | $callee | $duration | $bytes_in1 | $bytes_in2 | "; } $i++; } print " |